import 'package:architecture/os_size.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/db/isar.dart';

import '../../../widgets/text_button.dart';
import 'work_note_controller.dart';

class WorkNotePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetX<WorkNoteController>(
        init: WorkNoteController(),
        builder: (controller) {
          return Scaffold(
            appBar: AppBar(
              title: Text('工作日志'),
              actions: [
                TextButton(
                    onPressed: () {
                      controller.addRecord();
                    },
                    child: Text(
                      '添加记录',
                      style: TextStyle(color: Colors.white),
                    )),
              ],
            ),
            body: Container(
                margin: EdgeInsets.all(5),
                child: Row(
                  children: [
                    Column(
                      children: [
                        Text(controller.date.value),
                        _buildInputArea(controller),
                        Container(
                            margin: EdgeInsets.only(top: 10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.end,
                              children: [
                                _textButton('保存', onPressed: () {
                                  controller.save();
                                }),
                              ],
                            ))
                      ],
                    ),
                    _noteList(controller)
                  ],
                )),
          );
        });
  }

  _textButton(String text, {Function? onPressed}) {
    return Container(
        margin: EdgeInsets.only(left: 15, right: 20),
        alignment: Alignment.center,
        child: MyTextButton(
            text: text,
            confirm: () {
              onPressed?.call();
            },
            backgroundColor: Colors.deepPurpleAccent,
            sideColor: Colors.deepPurpleAccent,
            textStyle: TextStyle(color: Colors.white)));
  }

  _buildInputArea(WorkNoteController controller) {
    return Container(
        width: windowWidth * 0.5,
        height: windowHeight * 0.6,
        margin: EdgeInsets.symmetric(horizontal: 10, vertical: 5),
        child: TextField(
          // undoController: _undoController,
          controller: controller.textEditingController,
          expands: true,
          maxLines: null,
          minLines: null,
          textAlignVertical: TextAlignVertical.top,
          decoration: InputDecoration(
            filled: true,
            fillColor: Colors.transparent,
            hoverColor: Colors.transparent,
            border: const OutlineInputBorder(),
          ),
        ));
  }

  _noteList(WorkNoteController controller) {
    return Container(
        width: windowWidth * 0.45,
        margin: EdgeInsets.only(top: windowHeight * 0.03),
        child: ListView.separated(
          itemCount: controller.noteList.length,
          itemBuilder: (context, index) {
            final item = controller.noteList[index];
            return _itemLayout(item, controller);
          },
          separatorBuilder: (context, index) {
            return Container(
              height: 10,
            );
          },
        ));
  }

  _itemLayout(WorkNoteInfo note, WorkNoteController controller) {
    return Listener(
        onPointerMove: (movePointEvent) {},
        child: GestureDetector(
            onTap: () {},
            onLongPress: () {},
            onPanDown: (DragDownDetails details) {
              details.globalPosition.dx - 5;
            },
            child: ConstrainedBox(
                constraints: BoxConstraints(minHeight: 50), // 设置最小高度
                child: Container(
                    alignment: Alignment.centerLeft,
                    width: windowWidth * 0.15,
                    padding: EdgeInsets.only(left: 10),
                    decoration: BoxDecoration(
                      border: Border.all(color: Colors.deepPurpleAccent),
                    ),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          note.date,
                          style: TextStyle(
                              color: Colors.black, fontWeight: FontWeight.bold),
                        ),
                        // TextField(
                        //   // controller: controller.codeTextEditingController,
                        //   readOnly: true,
                        //   decoration: InputDecoration(
                        //     border: const OutlineInputBorder(),
                        //     hintText: note.content,
                        //   ),
                        //   maxLines: 30,
                        //   style: TextStyle(color: Colors.green),
                        // ),
                        SizedBox(
                          height: 10,
                        ),
                        Text(
                          note.content,
                          style: TextStyle(color: Colors.black),
                        ),
                        Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            TextButton(
                                onPressed: () {
                                  controller.copyContent(note.content);
                                },
                                child: Text('复制')),
                            TextButton(
                                onPressed: () {
                                  controller.deleteRecord(note.date);
                                },
                                child: Text(
                                  '删除',
                                  style: TextStyle(color: Colors.red),
                                ))
                          ],
                        )
                      ],
                    )))));
  }
}
